﻿// =================================================================== 
// 业务逻辑层(GDIT.BLL)
//====================================================================
// 文件：BInternalIP.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：内部IP分段文章操作逻辑
// ===================================================================
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using GDIT.Base.LogHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Model;

namespace GDIT.BLL
{
    public class BInternalIP
    {

        private Pattern.DALAbstractFactory dataFactory = DataProvider.defaultProvider;

        /// <summary>
        /// 更新内部IP分段
        /// </summary>
        /// <param name="ip">内部IP</param>
        /// <returns></returns>
        public bool UpdateInternalIP(Mtb_InternalIP ip)
        {
            Logger.Info(String.Format("内部IP段[{0}]-[{1}]", ip.IternalIPStart,ip.IternalIPEnd));
            int result = dataFactory.DALInternalIPProvider.UpdateInternalIP(ip);
            if (result == 1)
            {
                Logger.Info("Update success");
                return true;
            }
            else
            {
                Logger.Info("Update false");
                return false;
            }
        }

        /// <summary>
        /// 查询并返回内部IP分段
        /// </summary>
        /// <returns>True: 内网 False:外网</returns>
        public bool SelectInternalIP(string ip)
        {
            string[] arrUserIP = null;
            arrUserIP = ip.Split('.');
            XPCollection internalIP = dataFactory.DALInternalIPProvider.SelectInternalIP();
            foreach(Mtb_InternalIP i in internalIP)
            {
                string[] arrStart = null;
                string[] arrEnd = null;
                arrStart = i.IternalIPStart.Split('.');
                arrEnd = i.IternalIPEnd.Split('.');
                if (arrUserIP[0] == arrStart[0] && arrUserIP[0] == arrEnd[0])
                {
                    if (arrUserIP[1] == arrStart[1] && arrUserIP[1] == arrEnd[1])
                    {
                        if (arrUserIP[2] == arrStart[2] && Convert.ToInt32(arrUserIP[2]) <= Convert.ToInt32(arrEnd[2]))
                        {
                            return true;
                        }
                    }
                }
                else if (arrUserIP[0] == arrStart[0] && arrUserIP[0] == arrEnd[0])
                {
                    if (arrUserIP[1] == arrStart[1] && arrUserIP[1] == arrEnd[1] && arrUserIP[2] == arrStart[2] && arrUserIP[2] == arrEnd[2])
                    {
                        if (Convert.ToInt32(arrUserIP[3]) >= Convert.ToInt32(arrStart[3]) && Convert.ToInt32(arrUserIP[3]) <= Convert.ToInt32(arrStart[3]))
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        /// <summary>
        /// 新增内部IP分段
        /// </summary>
        /// <param name="ip">要新增的IP对象</param>
        /// <returns></returns>
        public bool AddInternalIP(Mtb_InternalIP ip)
        {
            Logger.Info(String.Format("内部IP段[{0}]-[{1}]", ip.IternalIPStart, ip.IternalIPEnd));
            int result = dataFactory.DALInternalIPProvider.AddInternalIP(ip);
            if (result == 1)
            {
                Logger.Info("Insert success");
                return true;
            }
            else
            {
                Logger.Info("Insert false");
                return false;
            }
        }

        /// <summary>
        /// 查询并返回所有的内部IP范围字段
        /// </summary>
        /// <returns></returns>
        public XPCollection SelectAllInternalIP()
        {
            return dataFactory.DALInternalIPProvider.SelectInternalIP();
        }
    }
}
